package com.mainframe;

import java.awt.Component;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;

import com.data.Message;
import com.data.TAG;
import com.operate.Operate;

public class MainFrame extends JFrame {

	private JButton btn_connect;
	private JButton btn_currentAdjust;
	private JButton btn_presureAdjust_noSource;
	private JButton btn_presureAdjust_Source;
	private JButton btn_startTest;
	private JButton btn_okTest;
	private JProgressBar pbr_operate;
	public  JComboBox cbx_chooseSerialPort;
	private JList list_message;
	private JList list_test;
	private JTextField tfd_test;
	private JLabel lbl_com;
	private JLabel lbl_message;
	private DefaultListModel listModel_message;
	private DefaultListModel listModel_test;
	private JScrollPane listScroll_test;
	private DlgCurrent dlgCurrent;
	private DlgPressrue dlgPressrue;
	private DlgPressureNo dlgPressureNo;

	public DefaultListModel getListModel_message() {
		return listModel_message;
	}

	public void setListModel_message(DefaultListModel listModel_message) {
		this.listModel_message = listModel_message;
	}

	public DefaultListModel getListModel_test() {
		return listModel_test;
	}

	public void setListModel_test(DefaultListModel listModel_test) {
		this.listModel_test = listModel_test;
	}

	public JList getList_message() {
		return list_message;
	}

	public void setList_message(JList list_message) {
		this.list_message = list_message;
	}

	public MainFrame() throws HeadlessException {
		super();
	}

	public void initComponents() {

		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("/******退出应用程序******/");
				setVisible(false);
				dispose();
				System.exit(0);
			}
		});
		btn_connect = new JButton("未连接设备");
		btn_connect.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Operate.getSerialcomm().connectComm();
				Message.msg.setTag(TAG.MSG_CONNECT);
				new Thread() {
					@Override
					public void run() {
						int count = 0;
						while (count++ < 4) {
							pbr_operate.setValue(25 * count);
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						// 更新4s进度条后，查看连接的状态
						if (Message.msg.isConnected()) {
							setButtonVisible(true);					
						}
					}
				}.start();
			}

		});
		btn_currentAdjust = new JButton("电流调整");
		btn_currentAdjust.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				DlgCurrent.dlg.setVisible(true);

			}
		});

		btn_presureAdjust_noSource = new JButton("无源压力调整");
		btn_presureAdjust_noSource.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DlgPressureNo.dlg.setVisible(true);
			}

		});
		btn_presureAdjust_Source = new JButton("有源压力调整");
		btn_presureAdjust_Source.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DlgPressrue.dlg.setVisible(true);
			}

		});
		btn_startTest = new JButton("开始标定");
		btn_startTest.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Message.msg.setTag(TAG.MSG_START_TEST);
			}

		});
		btn_okTest = new JButton("确认");
		btn_okTest.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(tfd_test.getText() != null){
					try {
						if(!tfd_test.getText().equals("")){
							System.out.println("OK:" + tfd_test.getText());
							float currect = Float.parseFloat(tfd_test.getText());
							Message.msg.setCvCurrent(currect);
							Message.msg.setTag(TAG.MSG_TEST_CURRENT);
						}
						
					} catch (Exception e2) {
						Message.msg.setRangeMsg(tfd_test.getText());
						Message.msg.setTag(TAG.MSG_TEST_RANGE);
						tfd_test.setText("");
					}
				}
			}

		});

		pbr_operate = new JProgressBar();
		int commsSize = Operate.getSerialcomm().getCommNum().size();
		String[] comms = new String[commsSize];
		if(commsSize != 0){
			for(int i=0; i<commsSize;i++){
				
				comms[i] = Operate.getSerialcomm().getCommNum().get(i);
			}
		}else{
			comms = new String[]{"none"};
		}
		cbx_chooseSerialPort = new JComboBox(comms);
		
		listModel_message = new DefaultListModel();
		listModel_test = new DefaultListModel();
		list_message = new JList(listModel_message);
		list_message.setCellRenderer(new ListRenderer());
		list_test = new JList(listModel_test);

		listScroll_test = new JScrollPane(list_message);

		tfd_test = new JTextField();
		lbl_com = new JLabel();
		lbl_message = new JLabel();
		dlgCurrent = new DlgCurrent(this);
		dlgPressrue = new DlgPressrue(this);
		dlgPressureNo = new DlgPressureNo(this);

		Container co = this.getContentPane();
		co.setLayout(null);

		lbl_com.setText("选择串口:");
		lbl_com.setBounds(new Rectangle(10, 20, 60, 30));
		cbx_chooseSerialPort.setBounds(new Rectangle(90, 20, 60, 30));

		btn_connect.setBounds(new Rectangle(180, 20, 80, 30));
		btn_currentAdjust.setBounds(new Rectangle(270, 20, 80, 30));
		btn_presureAdjust_noSource.setBounds(new Rectangle(360, 20, 120, 30));
		btn_presureAdjust_Source.setBounds(new Rectangle(500, 20, 120, 30));

		lbl_message.setText("变送器信息");
		lbl_message.setBounds(new Rectangle(10, 60, 70, 30));
		list_message.setBounds(new Rectangle(10, 90, 300, 450));
		list_test.setBounds(new Rectangle(340, 90, 420, 450));
		btn_startTest.setBounds(new Rectangle(340, 60, 80, 30));
		tfd_test.setBounds(new Rectangle(440, 60, 80, 30));
		btn_okTest.setBounds(new Rectangle(530, 60, 80, 30));
		pbr_operate.setBounds(new Rectangle(10, 550, 760, 10));

		co.add(lbl_com);
		co.add(cbx_chooseSerialPort);
		co.add(btn_connect);
		co.add(btn_currentAdjust);
		co.add(btn_presureAdjust_Source);
		co.add(btn_presureAdjust_noSource);
		co.add(btn_startTest);
		co.add(lbl_message);
		co.add(list_message);
		co.add(list_test);
		co.add(listScroll_test);
		co.add(pbr_operate);
		co.add(tfd_test);
		co.add(btn_okTest);

		setButtonVisible(false);
	}

	public void setButtonVisible(boolean state) {

		if (state) {
			btn_connect.setText("已连接设备");
		}
		btn_currentAdjust.setEnabled(state);
		btn_presureAdjust_noSource.setEnabled(state);
		btn_presureAdjust_Source.setEnabled(state);
		btn_startTest.setEnabled(state);
		btn_okTest.setEnabled(state);

	}

	 private class ListRenderer extends JLabel implements ListCellRenderer {
	        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
	            setText(value.toString());
	            return this;
	        }

	        public Insets getInsets() {
	            return new Insets(0,0,0,0);
	        }
	    }
	 
	public void scrollToView_Message() {
		int lastIndex = listModel_message.getSize();
		Rectangle rect = list_message.getCellBounds(lastIndex - 1, lastIndex - 1);
		listScroll_test.getViewport().scrollRectToVisible(rect);
	}

}
